﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp_Binding.Example
{
    /// <summary>
    /// ColumnChartWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ColumnChartWindow : Window
    {

        ChartDataSource chartDataSource = new ChartDataSource();

        public ColumnChartWindow()
        {
            InitializeComponent();
            this.DataContext = chartDataSource;
        }
    }

    public class ChartDataSource
    {

        public ObservableCollection<ColumnItem> columnItems { get; set; } = new ObservableCollection<ColumnItem>();

        public ChartDataSource()
        {
            for (int i = 0; i <= 15; i++)
            {
                columnItems.Add(new ColumnItem() 
                {
                    Value = new Random().Next(10,200),
                    Name = DateTime.Now.ToString("mm:ss")
                });
            }

            Task task = Task.Run(async () =>
            {
                while (true)
                {
                    await Task.Delay(1000);
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        columnItems.Add(new ColumnItem()
                        {
                            Value = new Random().Next(10, 200),
                            Name = DateTime.Now.ToString("mm:ss")
                        });
                        if (columnItems.Count > 20)
                        {
                            columnItems.RemoveAt(0);
                        }
                    });
                }
            });
        }
        

    }

    public class ColumnItem
    { 
        public int Value { get; set; }
        public string? Name { get; set; }    
    }
}
